home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 463 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  6.9 KB

  1. Path: chronicle.mti.sgi.com!austern
  2. From: rsm@gateway.Dtseng.Com (Roger S. Morris)
  3. Newsgroups: comp.std.c++
  4. Subject: Re: Generic Object Callbacks
  5. Date: 23 Feb 1996 09:45:18 PST
  6. Organization: Discrete Time Systems Corporation
  7. Approved: austern@isolde.mti.sgi.com
  8. Message-ID: <Dn8MAL.Evw@mv.mv.com>
  9. References: <pgpmoose.199602221531.14635@isolde.mti.sgi.com>
  10. NNTP-Posting-Host: isolde.mti.sgi.com
  11. X-Original-Date: Fri, 23 Feb 1996 16:39:57 GMT
  12. Apparently-To: comp-std-c++@uunet.mv.com
  13. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  14.     iQBVAwUBMS39Uky4NqrwXLNJAQFgVwH7BXtVNrdao2Du46qbMLFJ6oFFWjvVVjBg
  15.     N6J1dyhKl/3wpt9OMGxYmxOYHkO7oDzvxLVZYs3MPQWQ8mQxeE8F5g==
  16.     =QRFD
  17. Originator: austern@isolde.mti.sgi.com
  18.  
  19. Hi, We've had to address the same problem.
  20.  
  21. We've developed a general-purpose callback class that can be used to
  22. contain almost any  kind of callback.  This code is "under development"
  23. in that I'm not convinced that it has the cleanest-possible interface.
  24. The code works with the latest gnu compiler.
  25.  
  26. Why do I mention this in comp.std.c++?  Well, firstly in order to respond
  27. to the original poster.  Secondly, I'd like to mention that this sort
  28. of code would be much cleaner if the language had a notion of a
  29. ``parameter-list'' type, which would allow me to manipulate, store, and
  30. pass along any list of parameters.  Also, it would be nice if a function
  31. of return type ``void'' would be allowed to return a void.  For example,
  32.     void foo() {}
  33.     void bar() { return foo(); }
  34.  
  35. If anyone is interested in our code, I will place the code at the following
  36. http address today:  http://www.dtseng.com/sig/callback .
  37. Any comments would be really appreciated.
  38.  
  39. --Roger Morris
  40.  
  41.  
  42. Here are some of the comments from callback.H...
  43.  
  44. /// INTRODUCTION ////////////////////////////////////////////////////////////
  45. //
  46. // This file defines a general-purpose "CALLBACK HANDLE", an object that
  47. // represents everything a caller needs for making a function callback.
  48. // Here is an example:
  49. //     Suppose that class ``PushButton'' is some sort of graphical widget
  50. //     that performs a callback when "pushed".  It's ctor might look like:
  51. //         PushButton::PushButton( string name, Win window, callback0 cb );
  52. //     where ``cb'' represents the function to be called.
  53. //
  54. //     In order to have a function ``foo1'',
  55. //         void foo1();
  56. //     called by a PushButton ``pb1'', create ``pb1'' as follows:
  57. //         PushButton pb1(name,win, callback0(&foo1) );
  58. //     or simply:
  59. //         PushButton pb1(name,win, &foo1 );
  60. //
  61. //     In order to have a member function ``foo2'',
  62. //         struct S { void foo2(); } s2;
  63. //     called by a PushButton ``pb2'', create ``pb2'' as follows:
  64. //         PushButton pb2(name,win, callback0_mf<S>(&s2,&s2::foo2) );
  65. //         
  66. //     In order to have a function ``foo3'',
  67. //         void foo3( const bool& );
  68. //     called with ``true'' when ``pbT'' is pushed and with ``false'' 
  69. //     when ``pbF'' is pushed, create ``pbT'' and ``pbF'' as follows:
  70. //         PushButton pbT(name,win, callback0_tag<bool>(&foo3,true) );
  71. //         PushButton pbF(name,win, callback0_tag<bool>(&foo3,false) );
  72. //
  73. //     Lastly, suppose that ``PushButton'' actually passes a reference
  74. //     to an ``EventStruct'' to the callback.  In this case, its ctor
  75. //     might instead look like:
  76. //         PushButton::PushButton( string, Win, callback1<EventStruct> cb );
  77. //     Though the ctor interface has changed, the code above that initializes
  78. //     ``pb1'', ``pb2'', ``pbT'', and ``pbF'' WILL STILL WORK PERFECTLY FINE
  79. //     as coded (because a callback1<EventStruct> can be constructed from a
  80. //     callback0).  It is now possible, however, to have a function ``foo4'',
  81. //         void foo4( EventStruct& );
  82. //     called by a PushButton ``pb4'' by creating ``pb4'' as follows:
  83. //         PushButton pb4(name,win, callback1<EventStruct>(&foo4) );
  84. //     or simply:
  85. //         PushButton pb4(name,win, &foo4 );
  86. // The purpose of the "callback handle" is to separate the "caller" from
  87. // the "callee" as much as possible; neither should have to be concerned
  88. // about the irrelavent (from its point of view) details of the other.
  89. //
  90. /// OVERVIEW ////////////////////////////////////////////////////////////////
  91. //
  92. // Use of these callback handles allow a function callback to be made without
  93. // the caller having to know:
  94. //     - Whether the called function is a static (global) function or a member
  95. //       function,
  96. //     - What object the called function is a member of, if any,
  97. //     - What special callee-provided parameters must be passed, such as
  98. //       the ``void* tag'' parameter so-often used with Motif call-back 
  99. //       functions.
  100. // and without the callee having to:
  101. //     - Set up a special static function to wrap each member function in.
  102. //     - Bypass type checking by casting a ``void* tag'' argument.
  103. //     - Point a ``tag'' argument to a special intermediate class just to
  104. //       associate another piece of data with the callback.
  105. //
  106. // Other less-important features provided:
  107. //     - The callback handles are implemented as counted-reference handles,
  108. //       and so can be passed around or placed in lists without lots of
  109. //       copy constructor overhead.
  110. //     - The handles are default constructable, copy constructable, and
  111. //       equality comparable.  This allows easy use within STL containers.
  112. //     - The actual handle objects contain virtual member functions and a
  113. //       single data element (a pointer), so their size is likely to be
  114. //       only that of a couple of pointers.
  115. //       (Note, once the compiler supports templated member functions, it
  116. //       will be possible to reimplement this without virtual member 
  117. //       functions.)
  118. //     - The callee has the ability to invalidate its handles, and so
  119. //       cause an error to be thrown if a callback is attempted.
  120. //
  121. // Through use of these handles, a callback can receive up to 3 arguments:
  122. //     - "P1"  is caller-provided, 
  123. //        --          --
  124. //     - "P2"  is caller-provided, 
  125. //        --          --
  126. //     - "TAG"  is callee-provided (such as Motif's ``void* tag'' param).
  127. //        ---          --
  128. //
  129. // The callback's RETURN VALUE IS ALWAYS IGNORED.
  130. //
  131. // A caller which has parameters to pass will use a ``callback1<P1TYPE>''
  132. // or a ``callback2<P1TYPE,P2TYPE>'' handle.  A caller without
  133. // arguments will use a ``callback0'' handle. 
  134. //
  135. // If additional parameters or return data is needed, they must be grouped
  136. // into P1, P2, or TAG (perhaps with pair<..>).  (Alternatively, the user can
  137. // extend this framework to handle more params or a return value, but things
  138. // are already complicated enough.  Perhaps someday it will be possible in
  139. // C++ to parameterize parameter lists, not just individual parameters.)
  140. //
  141. ---
  142. [ To submit articles: Try just posting with your newsreader.  If that fails,
  143.                       use mailto:std-c++@ncar.ucar.edu
  144.   FAQ:    http://reality.sgi.com/employees/austern_mti/std-c++/faq.html
  145.   Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html
  146.   Comments? mailto:std-c++-request@ncar.ucar.edu 
  147. ]
  148.